﻿Imports System.Data
Imports System.Data.OleDb

Partial Class SharePhoto
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            'If Request.Params("ID") = "" Then Response.Redirect("SharePhoto.aspx")
            Dim myGUID = Guid.NewGuid()
            Dim newFileName As String = myGUID.ToString() & ".jpg"
            Dim fileLocationOnServerHardDisk = Request.MapPath("Photos") & "/" & newFileName
            f_Photo.SaveAs(fileLocationOnServerHardDisk)
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("SingleMomMemberConnectionString").ConnectionString)
            Dim SqlString As String = "SELECT * FROM SharePhoto"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            oleDbConn.Open()
            Dim dataReader = cmd.ExecuteReader()
            GridView1.DataSource = dataReader
            GridView1.DataBind()


        End If
    End Sub

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
            Dim myGUID = Guid.NewGuid()
            Dim newFileName As String = myGUID.ToString() & ".jpg"
            Dim fileLocationOnServerHardDisk = Request.MapPath("Photos") & "/" & newFileName
            f_Photo.SaveAs(fileLocationOnServerHardDisk)
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("SingleMomMemberConnectionString").ConnectionString)
            Dim SqlString As String = "Insert into SharePhoto(MemberName,AboutPhoto,Photo) Values (@f1,@f2,@f3)"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            cmd.Parameters.AddWithValue("@f2", tb_AboutPhoto.Text)
            cmd.Parameters.AddWithValue("@f3", "Photos/" & newFileName)
            oleDbConn.Open()
            cmd.ExecuteNonQuery()

            ''''''Now, fill the gridview on the page
            Dim fillComments As String = "SELECT * FROM SharePhoto"
            Dim fillCommentsCmd As OleDbCommand = New OleDbCommand(fillComments, oleDbConn)
            Dim dataReader = fillCommentsCmd.ExecuteReader()
            GridView1.DataSource = dataReader
            GridView1.DataBind()

        tb_MemberName.Text = ""
        tb_AboutPhoto.Text = ""


    End Sub

End Class
